#ifndef SOLUTION_H_
#define SOLUTION_H_
#ifndef NULL
#define NULL 0
#endif
struct ListNode
{

	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *removeNthFromEnd(ListNode *head, int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
		if(n<0)
			return NULL;

        ListNode *p = head, *q = head;
		for(int i=1;i<n;i++)
			q=q->next;

		if(!(q->next))
		{
			return head->next;
		}

		ListNode* pre = NULL;
		while(q->next)
		{
			pre = p;
			p = p->next;
			q = q->next;
		}

		pre->next = p->next;
		return head;
    }
};
#endif
